package wei.xinya.concurrent;

import java.time.LocalTime;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;

public class Driver {

    public static void main(String[] args) throws InterruptedException {
        int count = 5;
        CountDownLatch startSignal = new CountDownLatch(1);
        CountDownLatch doneSignal = new CountDownLatch(count);
        ConcurrentLinkedQueue<String> index = new ConcurrentLinkedQueue<>();

        // create and start threads
        for (int i = 0; i < count; ++i) {
            new Thread(new Worker(startSignal, doneSignal, index, LocalTime.now())).start();
        }

        doSomethingElse("开工了");              // don't let run yet
        startSignal.countDown();                    // let all threads proceed

        doneSignal.await();                         // wait for all to finish
        doSomethingElse("完工了，以下是工作效率排行：");
        index.forEach(System.out::println);
    }

    static void doSomethingElse(String note) {
        System.out.println(LocalTime.now() + " --- " + note);
    }

}